/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entity.KhachHang;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dark_Knight
 */
public class KhachHangImpl implements KhachHangDAO {

    public boolean add(KhachHang data) {
        String sql = "{call sp_KHACHHANG_add(?,?,?,?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        
        try {
            db.setOutParameter(1, java.sql.Types.INTEGER);
            db.setParameter(2, data.getMaNhanVien());
            db.setParameter(3, data.getHoTenKH());
            db.setParameter(4, data.getGioiTinh());
            db.setParameter(5, data.getDiaChiKH());
            db.setParameter(6, data.getDienThoaiKH());
            db.setParameter(7, data.getEmailKH());
            db.setParameter(8, data.getSoTienNo());
            db.executeStorePro();
            
            data.setMaKhachHang(db.getIntOutValue(1));
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean delete(int MaKhachHang) {
        String sql = "{call sp_KHACHHANG_delete(?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        
        try {
            db.setParameter(1, MaKhachHang);
            db.executeStorePro();            
            db.closed();
            return true;            
        } catch (SQLException ex) {
            Logger.getLogger(KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
        
    }

    public boolean edit(KhachHang data) {
        String sql = "{call sp_KHACHHANG_edit(?,?,?,?,?,?,?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        
        try {
            db.setParameter(1, data.getMaKhachHang());
            db.setParameter(2, data.getMaNhanVien());
            db.setParameter(3, data.getHoTenKH());
            db.setParameter(4, data.getGioiTinh());
            db.setParameter(5, data.getDiaChiKH());
            db.setParameter(6, data.getDienThoaiKH());
            db.setParameter(7, data.getEmailKH());
            db.setParameter(8, data.getSoTienNo());
            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public ResultSet getAll() {

        String sql = "{call sp_KHACHHANG_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        return db.excuteQuery();
    }

    public boolean addTienNo(int maKH, int tien) {
        String sql = "{call sp_KHACHHANG_addTienNo(?,?)}";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        try {
            db.setParameter(1, maKH);
            db.setParameter(2, tien);
            db.executeStorePro();
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public ResultSet getByID(int maKH) {
        String sql = "{call sp_KHACHHANG_getByID(?)}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        try {
            db.setParameter(1, maKH);
            return db.excuteQuery();
        } catch (SQLException ex) {
            Logger.getLogger(KhachHangImpl.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    
}
